<?php

namespace Common\Model;

/**
 * AdminLoginModel.class.php实例模型
 * 
 * @author songanwei
 * @date 2016-1-4
 */
class AdminLoginModel extends BaseModel {

    protected $trueTableName = 'tb_admin_login';

    /**
     * 手机号返回用户数据
     * @modify songanwei 2015-01-04
     * @param string $mobile 手机号
     * @return type
     */
    public function getInfoByMobile($mobile, $areaCode = '86') {
        return $this->where(array('mobile' => $mobile,'areacode' => $areaCode))->find();
    }

    /**
     * 添加用户
     * @modify songanwei 2016-01-04
     * @modify songanwei 2017-05-11 新增 $areaCode
     * @param string $mobile 手机号
     * @param string $password  密码
     * @param string $name  用户名称
     * @return type
     */
    public function addInfo($mobile, $password, $name, $status=null, $areaCode = '86') {
        $options = array(
            'mobile' => $mobile,
            'password' => $password,
            'name' => $name,
            'areacode' => $areaCode,
        );
        if($status){
            $options['status'] = $status; 
        }

        $userId = $addResult = $this->add($options);
        // 添加引导页数据
        $this->setGuidanceData($userId);

        return $addResult;
    }
	/**
     * 修改账号的试用期
     * @modify zhangzhen 2016-12-28
     * @param string $adminId 管理员Id
     * @return type
     */
    public function saveInfo($adminId, $data) {
        $options = array(
            'id' => $adminId,
        );
        //return $this->where($options)->save($data);
        $result = $this->where($options)->save($data);
        return $result === false ? false : true;
    }

    /**
     * 获取信息
     * @modify trainyao 2016-01-06 创建
     * @param  mixin   $adminId        管理员id(s)
     * @return array              信息数组
     */
    public function getInfo($adminId) {
        $option = array();

        if (is_array($adminId)) {
            $adminId = implode(',', $adminId);
        }
        if (strpos($adminId, ',') !== false) {
            $option['id'] = array('in', $adminId);
            $data = $this->where($option)->select();
        } else {
            $option['id'] = $adminId;
            $data = $this->where($option)->find();
        }

        return empty($data) ? array() : $data;
    }

    /**
     * 设置申请状态
     * @modify zhangzhen 2016-12-29
     * @param integer $userId  用户ID
     * @param string $endtime  
     * @return type
     */
    public function setStatus($userId,$status){
        $result = $this->where(array('id'=>$userId))->save(array('status'=>$status));
        return $result === false ? false : true;
    }

    /**
     * 记录redis
     * @modify wenhui 2017-11-20    创建
     */
    public function setGuidanceData($userId) {
        $guiRedisUtil = new \Redis();
        $guiRedisUtil->connect(C('REDIS_SERVERIP'), C('REDIS_SERVERPORT'));
        $guiRedisUtil->auth(C('REDIS_AUTH_PASSWORD'));
        $guiRedisUtil->select(C('REDIS_GUIDANCE_DB'));

        $firstFeild = 'first_' . $userId;
        $firstResult = $guiRedisUtil->hset('guidance', $firstFeild, 1);
        return;
    }
}
